package com.cloud.easyexcel.service.impl;

import com.alibaba.excel.EasyExcel;
import com.cloud.easyexcel.entity.SysProductBom;
import com.cloud.easyexcel.entity.SysProductBomExcel;
import com.cloud.easyexcel.service.SysProductBomService;
import com.cloud.easyexcel.utils.BeanConverUtil;
import com.cloud.easyexcel.utils.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author HYZ
 * @description sys_product_bom
 * @date 2024-06-20
 */
@Service
@Slf4j
public class SysProductBomServiceImpl implements SysProductBomService {


    @Override
    public void downloadTemplate(HttpServletResponse response) {
        String fileName = "导入BOM模板";
        String sheetName = "导入BOM模板";
        List<SysProductBomExcel> assemblyTemplates = new ArrayList<>();
        assemblyTemplates.add(new SysProductBomExcel());
        try {
            ExcelUtil.writeExcel(response, null, fileName, sheetName, SysProductBomExcel.class);
        } catch (Exception e) {
            log.error("BOM模板导出异常-->", e);
        }
    }

    @Override
    public void importTemplateData(MultipartFile file, Integer versionId) {

        try {
            List<SysProductBomExcel> excels = EasyExcel.read(new BufferedInputStream(file.getInputStream())).head(SysProductBomExcel.class).sheet().doReadSync();
            List<SysProductBom> sysProductBoms = BeanConverUtil.convertList(excels, SysProductBom.class);
            sysProductBoms.forEach(sysProductBom -> {
                sysProductBom.setVersionId(versionId);
                sysProductBom.setStatus(1);
            });
            // 数据处理
//            this.batchInsert(sysProductBoms);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public String export(HttpServletResponse response) throws Exception {

        List<SysProductBomExcel> res = new ArrayList<>();
        SysProductBomExcel sysProductBomExcel = new SysProductBomExcel();
        sysProductBomExcel.setCategory("Category");
        sysProductBomExcel.setQuantity("1");
        sysProductBomExcel.setMaterielCode("MaterielCode");
        res.add(sysProductBomExcel);
        ExcelUtil.writeExcel(response, res, "导入BOM模板", "导入BOM模板", SysProductBomExcel.class);
        return "success";
    }
}